<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 班课频道
*/
class Banke extends MY_Controller {


	/**
	** 班课详情
	*/
	public function detail($id)
	{
		//获取课程信息
		$this->load->model("Courseo2o_model");
		$course = $this->Courseo2o_model->get_course_detail($id);

		if(empty($course)){
			header("Location:/banke");exit;
		}
		$this->smarty->assign("course", $course);
		
		//课程科目
		$subject_list = $this->Courseo2o_model->get_course_subject_list($id);
		$this->smarty->assign("subject_list", $subject_list);

		//获取学校信息
		$this->load->model("School_model");
		$school = $this->School_model->get_school_by_id($course['school_id']);
		$this->smarty->assign("school", $school);

		//获取校区
		$this->load->model('School_model');
		$campus = $this->School_model->get_school_campus($this->school_id);
		$this->smarty->assign("campus", $campus);

		$this->smarty->assign("menutag", "course");
		$this->smarty->display("banke/detail.html");
	}

	/**
	** 面授课报名
	*/
	public function baoming($course_id)
	{	
		
		$this->smarty->assign("course_id", $course_id);

		$post_data = $this->input->post();
		if($post_data){
			//提交报名
			$this->load->model("Order_model");
			$order_id = $this->Order_model->banke_signup($this->user_data['id'],$course_id,$post_data);
			if($order_id>0){
				header("Location:/banke/success/{$order_id}");exit;
			}
			// print_r($post_data);exit;
		}
		if($this->user_data['id']){

			//获取学员信息
			$this->load->model('Student_model');
			$student = $this->Student_model->get_student_by_userid($this->user_data['id']);
			if(empty($student)){
				$student['name']='';
				$student['phone'] = $this->user_data['mobile'];
				$student['birthday']="";
				$student['sex'] = 0;
				$student['wx'] = "";
				$student['qq'] = "";
				$student['edu'] = 0;
				$student['on_job'] = -1;
				$student['address'] = '';
				$student['remark'] = '';
			}
	  		$this->smarty->assign("student", $student);
	  	}

	  	//获取课程信息
		$this->load->model("Courseo2o_model");
		$course = $this->Courseo2o_model->get_course_detail($course_id);

		if(empty($course)){
			header("Location:/banke");exit;
		}

		$this->smarty->assign("course", $course);

  		//获取校区
		$this->load->model('School_model');
		$campus = $this->School_model->get_school_campus($this->school_id);
		$this->smarty->assign("campus", $campus);
  		

		$this->smarty->display("banke/baoming.html");

		// $post_data = $this->input->post();
		// $this->load->model('Order_model');
		// $settle_id = $this->Order_model->buy_now($this->user_data['id'],$post_data['course_id'],$post_data['num']);
		// if($settle_id>0){
		// 	header("Location:/order/settle/{$settle_id}");exit;
		// }
	}


	/**
	 * 报名成功跳转
	 * @param  [type] $order_id [description]
	 * @return [type]           [description]
	 */
	public function success($order_id)
	{

		$this->load->model('Order_model');
    	$order = $this->Order_model->get_order_by_id($order_id);
		if(empty($order)){
			header("Location:/");exit;
		}
		if($this->logged_in==0){
			header("Location:/logreg");exit;
		}

		if($order['buyer_user_id']!=$this->user_data['id']){
			print_r("不是你的订单");
			exit;
		}

    	$this->smarty->assign("order", $order);

    	//查询学校信息
		$this->load->model('School_model');
		$school = $this->School_model->get_school_by_id($this->school_id);
		$this->smarty->assign("school", $school);

		//查询校区信息 school_campus_id
		$school_campus = $this->School_model->get_school_campus_by_id($order['school_campus_id']);
		$this->smarty->assign("school_campus", $school_campus);

		$this->smarty->display("banke/baoming_success.html");
	}

	/**
	 * 面授课list
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function index($cate_id='',$limit=0)
	{	
		$offset = 10;
		$this->smarty->assign("cate_id", $cate_id);

		//面授课分类
		$this->load->model('Courseo2o_model');
    	$cate_list = $this->Courseo2o_model->get_offline_cate($this->school_id);
		$this->smarty->assign("cate_list", $cate_list);

		//查询面授课
		$list = $this->Courseo2o_model->get_banke_list_page($this->school_id,$cate_id,$limit,$offset);
		$this->smarty->assign("course_list", $list['result']);

		$page_config['uri_segment'] = 4;
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$page_config['base_url'] = '/banke/index/'.$cate_id;
		$page_config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);



		$this->smarty->assign("menutag", "course");
		$this->smarty->display("banke/list.html");
	}
}
